package com.se.global;

import cn.hutool.http.HttpStatus;
import com.se.service.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

@Slf4j
@Component
public class AuthHeaderSettingFilter implements Filter {
	
	@Autowired
	private AuthService authService;
	@Value("${auth.user-token.name}")
	private String userTokenName;
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		Filter.super.init(filterConfig);
	}
	
	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
		/*
		 request.getRequestURI(): /api/no/book/v1/bookInfo/get/1
		 request.getRequestURL(): http://localhost:8080/api/no/book/v1/bookInfo/get/1
		 request.getContextPath(): /api
		 request.getServletPath(): /no/book/v1/bookInfo/get/1
		 request.getPathInfo(): null
		 log.info("request.getRequestURI(): {}",request.getRequestURI());
		 log.info("request.getRequestURL(): {}",request.getRequestURL());
		 log.info("request.getContextPath(): {}",request.getContextPath());
		 log.info("request.getServletPath(): {}",request.getServletPath());
		 log.info("request.getPathInfo(): {}",request.getPathInfo());
		 */
		
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		
		String servletPath = request.getServletPath();
		int authOffset = servletPath.indexOf("/",1);
		if(authOffset == -1){
			response.sendError(HttpStatus.HTTP_NOT_FOUND);
			return;
		}
		String authPath = servletPath.substring(1,authOffset);
		HttpSession session = request.getSession();
		switch (authPath){
			case "no":
			case "callback":
				break;
			case "need":{
				Long userId = this.getUserIdFromSession(session);
				request = new AuthHeaderRequestWrapper(request, userId);
				break;
			}
			case "must":{
				
				Long userId = this.getUserIdFromSession(session);
				if(userId == null){
					response.sendError(HttpStatus.HTTP_FORBIDDEN);
					return;
				}
				request = new AuthHeaderRequestWrapper(request, userId);
				break;
			}
			default:{
				response.sendError(HttpStatus.HTTP_NOT_FOUND);
				return;
			}
			
		}
		
		chain.doFilter(request, response);
	}
	
	private Long getUserIdFromSession(HttpSession session){
		String userToken = (String) session.getAttribute(this.userTokenName);
		if(userToken == null || userToken.isEmpty()){
			return null;
		}
		Long userId = authService.getUserIdFromToken(userToken);
		if(userId == null){
			session.removeAttribute(this.userTokenName);
		}
		return userId;
	}
	@Override
	public void destroy() {
		Filter.super.destroy();
	}

	
}


